/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-07-01
 * Time: 21:17
 */
public class test1 {
    public int trap(int[] height) {
        int n = height.length;
        int maxHeight = 0;
        int maxHeightIndex = -1;
        for(int i=0;i<n;i++){
            if(height[i]>maxHeight){
                maxHeight = height[i];
                maxHeightIndex = i;
            }
        }
        if(maxHeightIndex==-1) {
            return 0;
        }
        int maxWater = 0;
        int currentHeight = 0;
        for(int i=0;i<maxHeightIndex;i++){
            currentHeight = Math.max(currentHeight,height[i]);
            maxWater += currentHeight-height[i];
        }
        currentHeight = 0;
        for(int i=n-1;i>maxHeightIndex;i--){
            currentHeight = Math.max(currentHeight,height[i]);
            maxWater += currentHeight-height[i];
        }
        return maxWater;
    }
}
